import tkinter as tk
from tkinter import messagebox
from tkinter import ttk
from tkinter.ttk import Treeview
import show_grade
import testMysql
import pymysql
import Matplotlib
import excel
import show_c
class ChangFrame(tk.Frame):
    def __init__(self,root):
        super().__init__(root)
        self.sno = tk.StringVar()
        self.cno = tk.StringVar()
        self.grade = tk.StringVar()
        self.status = tk.StringVar()
        self.create_page()
    def show_sc(self):


        # colums=("科目","课号","学号","成绩")
        # self.b1=ttk.Treeview(self,height=18,show="headings",colums=colums)
        # self.b1.pack()
        list = []
        list = testMysql.DB.SC(self)
        colums = ("科目", "课号","学号","成绩")
        self.b1 = ttk.Treeview(self, height=18, show="headings", column=colums)
        self.b1.grid(row=5, column=2, pady=10)
        self.b1.column("科目", width=200, anchor="center")
        self.b1.column("课号", width=200, anchor="center")
        self.b1.column("学号", width=200, anchor="center")
        self.b1.column("成绩", width=200, anchor="center")

        self.b1.heading("科目", text="科目")
        self.b1.heading("课号", text="课号")
        self.b1.heading("学号", text="学号")
        self.b1.heading("成绩", text="成绩")
        # list = [("s", "e"), ("1", "2")]
        # self.b1.insert("", "end", values=list[0])
        # self.b1.insert("", "end", values=list[1])
        for i in list:
            self.b1.insert("","end",values=i)
        self.VS = ttk.Scrollbar(self, orient="vertical", command=self.b1.yview)

        self.VS.place(relx=0.99, rely=0.35, relwidth=0.024, relheight=0.758)

        self.b1.configure(yscrollcommand=self.VS.set)





    def create_page(self):
        tk.Label(self).grid(row=0, pady=10)

        tk.Label(self, text="学号: ").grid(row=1, column=1, pady=10)
        tk.Entry(self, textvariable=self.sno).grid(row=1, column=2, pady=10)

        tk.Label(self, text="课号: ").grid(row=2, column=1, pady=10)
        tk.Entry(self, textvariable=self.cno).grid(row=2, column=2, pady=10)

        tk.Label(self, text="成绩: ").grid(row=3, column=1, pady=10)
        tk.Entry(self, textvariable=self.grade).grid(row=3, column=2, pady=10)

        tk.Button(self, text="修改", command=self.change_info).grid(row=4, column=2, pady=10)
        self.show_sc()

    def change_info(self):
        a = self.sno.get()
        b = self.cno.get()
        c = self.grade.get()
        testMysql.DB.changing(a,b,c)
        self.show_sc()
        messagebox.showinfo("Message","修改成功")


class InsertFrame(tk.Frame):
    def __init__(self,root):
        super().__init__(root)
        self.sno=tk.StringVar()
        self.cno=tk.StringVar()
        self.grade=tk.StringVar()
        self.status=tk.StringVar()

        self.create_page()

    def show_s(self):

        list = []
        list = testMysql.DB.S(self)
        colums = ("姓名", "学号", "课号", "成绩")
        self.b1 = ttk.Treeview(self, height=18, show="headings", column=colums)
        self.b1.grid(row=5, column=2, pady=10)
        self.b1.column("姓名", width=200, anchor="center")
        self.b1.column("学号", width=200, anchor="center")
        self.b1.column("课号", width=200, anchor="center")
        self.b1.column("成绩", width=200, anchor="center")

        self.b1.heading("姓名", text="姓名")
        self.b1.heading("学号", text="学号")
        self.b1.heading("课号", text="课号")
        self.b1.heading("成绩", text="成绩")
        # list = [("s", "e"), ("1", "2")]
        # self.b1.insert("", "end", values=list[0])
        # self.b1.insert("", "end", values=list[1])
        for i in list:
            self.b1.insert("", "end", values=i)
        self.VS = ttk.Scrollbar(self, orient="vertical", command=self.b1.yview)

        self.VS.place(relx=0.99, rely=0.35, relwidth=0.024, relheight=0.758)

        self.b1.configure(yscrollcommand=self.VS.set)


    def create_page(self):
        tk.Label(self).grid(row=0,pady=10)

        tk.Label(self,text="学号: ").grid(row=1,column=1,pady=10)
        tk.Entry(self,textvariable=self.sno).grid(row=1,column=2,pady=10)

        tk.Label(self, text="课号: ").grid(row=2, column=1, pady=10)
        tk.Entry(self, textvariable=self.cno).grid(row=2, column=2, pady=10)

        tk.Label(self, text="成绩: ").grid(row=3, column=1, pady=10)
        tk.Entry(self, textvariable=self.grade).grid(row=3, column=2, pady=10)

        tk.Button(self,text="录入",command=self.recode_info).grid(row=4,column=2,pady=10)
        tk.Label(self,textvariable=self.status).grid(row=5,column=2,pady=10)
        self.show_s()

    def recode_info(self):
        a = self.sno.get()
        b = self.cno.get()
        c = self.grade.get()
        d=3
        d=testMysql.DB.insert(a, b, c)
        if d==0:
            messagebox.showinfo("Message", "该课程已经录入")
        else:
            self.show_s()
            messagebox.showinfo("Message", "录入成功")

class SearchFrame(tk.Frame):
    def __init__(self,root):
        super().__init__(root)
        self.sno = tk.StringVar()
        self.key = tk.StringVar()
        self.status = tk.StringVar()

        self.create_page()

    def create_page(self):
        tk.Label(self).grid(row=0, pady=10)

        tk.Label(self, text="学号: ").grid(row=1, column=1, pady=10)
        tk.Entry(self, textvariable=self.sno).grid(row=1, column=2, pady=10)

        # tk.Label(self, text="密码: ").grid(row=2, column=1, pady=10)
        # tk.Entry(self, textvariable=self.key).grid(row=2, column=2, pady=10)

        tk.Button(self, text="查询", command=self.search_info).grid(row=3, column=2, pady=10)
        # tk.Label(self, textvariable=self.status).grid(row=4, column=2, pady=10)

    def search_info(self):
        a=self.sno.get()
        b=self.key.get()
        # DBHOST = 'localhost'
        # DBUSER = 'root'
        # DBPASS = '123456'
        # DBNAME = 'test'
        # db = pymysql.connect(host=DBHOST, user=DBUSER, password=DBPASS, database=DBNAME)
        # cursor = db.cursor()
        # sql = "select * from S;"
        # cursor.execute(sql)
        # results = cursor.fetchall()
        # dict = {}
        # for row in results:
        #     dict.update({row[0]: row[5]})
        # TK = dict[a]
        # print(TK)
        # if TK==b:
        #     list = []
        list = testMysql.DB.search(a, b)
        show_grade.show.show_grades(list)
        print(list)

        # else:
        #     messagebox.showinfo("Message", "密码错误")

class DeleteFrame(tk.Frame):
    def show_s(self):

        list = []
        list = testMysql.DB.S(self)
        colums = ("姓名", "学号", "课号", "成绩")


        self.b1 = ttk.Treeview(self, height=18, show="headings", column=colums)




        self.b1.grid(row=5, column=2, pady=10)
        self.b1.column("姓名", width=200, anchor="center")
        self.b1.column("学号", width=200, anchor="center")
        self.b1.column("课号", width=200, anchor="center")
        self.b1.column("成绩", width=200, anchor="center")

        self.b1.heading("姓名", text="姓名")
        self.b1.heading("学号", text="学号")
        self.b1.heading("课号", text="课号")
        self.b1.heading("成绩", text="成绩")
        # list = [("s", "e"), ("1", "2")]
        # self.b1.insert("", "end", values=list[0])
        # self.b1.insert("", "end", values=list[1])
        for i in list:
            self.b1.insert("", "end", values=i)


        self.VS = ttk.Scrollbar(self, orient="vertical", command=self.b1.yview)

        self.VS.place(relx=0.99, rely=0.28, relwidth=0.024, relheight=0.758)

        self.b1.configure(yscrollcommand=self.VS.set)
    def __init__(self,root):
        super().__init__(root)
        self.sno = tk.StringVar()
        self.cno = tk.StringVar()
        self.status = tk.StringVar()

        tk.Label(self).grid(row=0,pady=10)

        tk.Label(self, text="学号: ").grid(row=1, column=1, pady=10)
        tk.Entry(self, textvariable=self.sno).grid(row=1, column=2, pady=10)

        tk.Label(self, text="课号: ").grid(row=2, column=1, pady=10)
        tk.Entry(self, textvariable=self.cno).grid(row=2, column=2, pady=10)

        tk.Button(self, text="删除", command=self.delete).grid(row=4, column=2, pady=10)
        tk.Label(self, textvariable=self.status).grid(row=5, column=2, pady=10)
        self.show_s()
    def delete(self):
        a = self.sno.get()
        b = self.cno.get()
        testMysql.DB.delete(a, b)
        self.show_s()
        messagebox.showinfo("Message", "删除成功")

class SettingFrame(tk.Frame):

    def __init__(self,root):
        super().__init__(root)
        self.cname = tk.StringVar()
        self.cno = tk.StringVar()
        self.credit = tk.StringVar()
        self.status = tk.StringVar()

        self.create_page()
    def show_c(self):
        list = []
        list = testMysql.DB.C(self)
        colums = ("课号", "科目", "学分")
        self.b1 = ttk.Treeview(self, height=18, show="headings", column=colums)
        self.b1.grid(row=5, column=2, pady=10)
        self.b1.column("课号", width=200, anchor="center")
        self.b1.column("科目", width=200, anchor="center")
        self.b1.column("学分", width=200, anchor="center")


        self.b1.heading("课号", text="课号")
        self.b1.heading("科目", text="科目")
        self.b1.heading("学分", text="学分")

        # list = [("s", "e"), ("1", "2")]
        # self.b1.insert("", "end", values=list[0])
        # self.b1.insert("", "end", values=list[1])
        for i in list:
            self.b1.insert("", "end", values=i)
    def create_page(self):
        tk.Label(self).grid(row=0, pady=10)

        tk.Label(self, text="科目: ").grid(row=1, column=1, pady=10)
        tk.Entry(self, textvariable=self.cname).grid(row=1, column=2, pady=10)

        tk.Label(self, text="课号: ").grid(row=2, column=1, pady=10)
        tk.Entry(self, textvariable=self.cno).grid(row=2, column=2, pady=10)

        tk.Label(self, text="学分: ").grid(row=3, column=1, pady=10)
        tk.Entry(self, textvariable=self.credit).grid(row=3, column=2, pady=10)

        tk.Button(self, text="添加", command=self.recode_info).grid(row=4, column=2, pady=10)
        tk.Label(self, textvariable=self.status).grid(row=5, column=2, pady=10)
        self.show_c()
    def recode_info(self):
        a=self.cname.get()
        b=self.cno.get()
        c=self.credit.get()
        testMysql.DB.setting(a,b,c)
        self.show_c()
        messagebox.showinfo("Message","添加成功")



class PrintFrame(tk.Frame):
    def __init__(self, root):
        super().__init__(root)
        self.cname = tk.StringVar()
        self.status = tk.StringVar()

        self.create_page()


    def create_page(self):
        tk.Label(self).grid(row=0, pady=10)

        tk.Label(self, text="科目: ").grid(row=1, column=1, pady=10)
        tk.Entry(self, textvariable=self.cname).grid(row=1, column=2, pady=10)



        tk.Button(self, text="打印", command=self.recode_info).grid(row=4, column=2, pady=10)
        tk.Label(self, textvariable=self.status).grid(row=5, column=2, pady=10)


    def recode_info(self):
        a = self.cname.get()
        list=[]
        list=testMysql.DB.excel(a)
        excel.write.write_excel(list)
        messagebox.showinfo("Message","打印成功")


class StatisticsFrame(tk.Frame):
    def __init__(self, root):
        super().__init__(root)
        self.depart = tk.StringVar()
        self.major = tk.StringVar()
        self.classroom = tk.StringVar()
        self.status = tk.StringVar()

        self.create_page()

    def create_page(self):
        tk.Label(self).grid(row=0, pady=10)

        tk.Label(self, text="系部: ").grid(row=1, column=1, pady=10)
        tk.Entry(self, textvariable=self.depart).grid(row=1, column=2, pady=10)

        tk.Label(self, text="专业: ").grid(row=2, column=1, pady=10)
        tk.Entry(self, textvariable=self.major).grid(row=2, column=2, pady=10)

        tk.Label(self, text="班级: ").grid(row=3, column=1, pady=10)
        tk.Entry(self, textvariable=self.classroom).grid(row=3, column=2, pady=10)

        tk.Button(self, text="统计", command=self.recode_info).grid(row=4, column=2, pady=10)
        tk.Label(self, textvariable=self.status).grid(row=5, column=2, pady=10)

    def recode_info(self):
        a=self.depart.get()
        b=self.major.get()
        c=self.classroom.get()
        list=[]
        list=testMysql.DB.grade(a,b,c)
        Matplotlib.mat.show(list)
        messagebox.showinfo("Message", "统计成功")







